<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Admin;

class UserController extends Controller
{

    // 上传头像
    public function set_usericon(Request $request){
           
        // 1 获取上传图片
        $file = $request->file("pic");
        $path = \base_path("/public/upload/");
          
        // 2 上传头像 
        $ext = $file->getClientOriginalExtension();
        $picName = time().rand(0,999).".".$ext;
        $file->move($path,$picName);

        // 3 保存到数据库表 
        $token = $request->bearerToken() ? $request->bearerToken():$request->token;
        $user = Admin::where('token',$token)->first();
        $user->icon = $picName;
        $bool = $user->save();

        // 用户反馈
        if($bool){
            return response()->json(['meta'=>['msg'=>'头像上传成功!','status'=>200]]);
        }else{
            return response()->json(['meta'=>['msg'=>'头像上传失败!','status'=>3000]]);
        }       
           
    }


    // 修改信息
    public function edit(Request $request){

        $user = Admin::find($request->id);
        $user->email = $request->email;
        $user->password = bcrypt($request->password);
        $res = $user->save();

        if($res){
            return response()->json(['meta'=>['msg'=>'修改成功!','status'=>200]]);
        }else{
            return response()->json(['meta'=>['msg'=>'修改失败!','status'=>400]]);
        }

    }

    // 删除用户
    public function del($id){

        $user = Admin::find($id);
        $res = $user->delete();

        if($res){
            return response()->json(['meta'=>['msg'=>'删除成功!','status'=>200]]);
        }else{
            return response()->json(['meta'=>['msg'=>'删除失败!','status'=>400]]);
        }

    }

    // 更新状态
    public function update(Request $request){

        $user = Admin::find($request->id);
        $user->state = $request->state;
        $res = $user->save();
        if($res){
            return response()->json(['meta'=>['msg'=>'修改成功!','status'=>200]]);
        }else{
            return response()->json(['meta'=>['msg'=>'修改失败!','status'=>400]]);
        }

    }

    // 获取单个用户信息
    public function user($id){

        $user = Admin::find($id);
        return response()->json(['meta'=>['msg'=>'修改成功!','status'=>200],'data'=>$user]);

    }

    // 用户列表
    public function users(Request $request){

        $words = '%'.$request->words.'%';
        $data = Admin::where("username","like",$words)->orderBy('id','desc')->paginate($request->pagesize);
        return response()->json(['meta'=>['status'=>200,'msg'=>'获取成功'],
                                          'data'=>$data]);
    }

    // 添加用户
    public function adduser(Request $request){

        $data = $request->all();
        $token = md5(uniqid(md5(microtime(true)),true));
        $user = Admin::create([
            'username' => $data['username'],
            'email'=>$data['email'],
            'password' => bcrypt($data['password']),
            'token' =>$token
        ]);
        return response()->json(['meta'=>['status'=>201,'msg'=>'用户添加成功'],'data'=>$user]);

    }
   
    // 重置密码
    public function reset_password(Request $request){
       
        $token = $request->bearerToken() ? $request->bearerToken():$request->token;
        $user = Admin::where('token',$token)->first();
        $user->password = bcrypt($request->password);
        $bool = $user->save();

        if($bool){
            return response()->json(['meta'=>['status'=>200,'msg'=>'密码重置成功']]);
        }else{
            return response()->json(['meta'=>['status'=>200,'msg'=>'密码重置失败']]);
        }
        
    }

     // 头像上传
     public function upload_icon(Request $request){
       
        $token = $request->bearerToken() ? $request->bearerToken():$request->token;
        $user = Admin::where('token',$token)->first();
        $user->password = bcrypt($request->password);
        $bool = $user->save();
        
        if($bool){
            return response()->json(['meta'=>['status'=>200,'msg'=>'密码重置成功']]);
        }else{
            return response()->json(['meta'=>['status'=>200,'msg'=>'密码重置失败']]);
        }
        
    }


    //用户登录
    public function login(Request $request){

        $params = \request(["username","password"]);

        $data = Admin::where("username",$request->username)->first();

        $data->icon =  config('app.upload').$data->icon;

        if(\Auth::guard("admin")->attempt($params)){
            return response()->json(['meta'=>['status'=>200,'msg'=>'登录成功!'],'data'=>$data]);
        }else{
           return response()->json(['meta'=>['status'=>400,'msg'=>'账号或密码错误!']]);
        }

    }
    
    // 关注房源 
    public function like(Request $request){ 

        $token = $request->bearerToken() ? $request->bearerToken():$request->token;
        
        $adminUser = Admin::where('token',$token)->first();

        $adminUser->my_likes()->attach($request->id);
        return response()->json(['meta'=>["msg"=>"关注成功","status"=>200]]);
        
    }

    // 取消关注 
    public function unlike(Request $request){

        $token = $request->bearerToken() ? $request->bearerToken():$request->token;
        $adminUser = Admin::where('token',$token)->first();
        $adminUser->my_likes()->detach($request->id);
        return response()->json(['meta'=>["msg"=>"取消关注成功","status"=>200]]);

    }

    // 获取封面图片
    private  function getCoverImg($str){
        $imgArr = explode("&", $str);
        return config('app.upload').'s_'.$imgArr[0]; // 默认第一张图片为封面图片;
    }
    
     // 我的关注 
     public function my_likes(Request $request){
         $token = $request->bearerToken() ? $request->bearerToken():$request->token;
        $adminUser = Admin::where('token',$token)->first();
        $houses = $adminUser->my_likes()->paginate($request->size);
        foreach($houses as $k=>$house){
            $houses[$k]["coverurl"]= $this->getCoverImg($house["imgs"]);
            unset($houses[$k]["imgs"]);
        }
        
        return response()->json(['meta'=>["msg"=>"获取成功","status"=>200],'data'=>$houses]);
    }
    
}
